// pages/confirm/confirm.js
const apiserverUrl = require('../../config').apiserverUrl;
const UpdateUserInfoUrl = require('../../config').UpdateUserInfoUrl;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: '',
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    postData: {}//回传用户信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ openid: options.openid });
    var _this = this;
    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              _this.data.postData = {
                openid: _this.data.openid,
                nickname: res.userInfo.nickName,
                gender: res.userInfo.gender,
                city: res.userInfo.city,
                province: res.userInfo.province,
                country: res.userInfo.country,
                avatarUrl: res.userInfo.avatarUrl,
                language: res.userInfo.language
              };
            }
          })
        }
      }
    })
  },
  bindGetUserInfo: function (res) {
    var _this = this;
    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {//已授权
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              _this.data.postData = {
                openid: _this.data.openid,
                nickname: res.userInfo.nickName,
                gender: res.userInfo.gender,
                city: res.userInfo.city,
                province: res.userInfo.province,
                country: res.userInfo.country,
                avatarUrl: res.userInfo.avatarUrl,
                language: res.userInfo.language
              };
            }
          })
        }
        else {//未授权
          wx.openSetting({//重新授权
            success: function (data) {
              if (data) {
                if (data.authSetting["scope.userInfo"] == true) {                 
                  wx.getUserInfo({                   
                    success: function (res) {
                      _this.data.postData = {
                        openid: _this.data.openid,
                        nickname: res.userInfo.nickName,
                        gender: res.userInfo.gender,
                        city: res.userInfo.city,
                        province: res.userInfo.province,
                        country: res.userInfo.country,
                        avatarUrl: res.userInfo.avatarUrl,
                        language: res.userInfo.language
                      };
                    },
                    fail: function () {
                      wx.showToast({
                        title: '授权设置失败',
                        icon: 'fail',
                        duration: 3000
                      });             
                    }
                  });
                }
              }
            },
            fail: function () {
              wx.showToast({
                title: '授权设置失败',
                icon: 'fail',
                duration: 3000
              });             
            }
          });
        }
      }
    })    
  },
  formSubmit: function (e) {
    var _this = this;
    var mobile = e.detail.value.confirmTel;
    if (mobile == "") {
      wx.showToast({
        title: '请输入手机号',
        icon: 'fail',
        duration: 3000
      });
    } else {
      
      console.log(_this.data.postData);
      if (_this.data.postData != null && _this.data.postData != "" && JSON.stringify(_this.data.postData) != "{}") {
        _this.data.postData['mobile'] = mobile;
        wx.showLoading({
          title: '认证中'         
        }); 
        wx.request({
          //获取openid接口  
          url: apiserverUrl,
          data: { method: "GET", url: UpdateUserInfoUrl, data: _this.data.postData },
          method: 'POST',
          success: res => {
            wx.hideLoading();
            if (res.data.IsSuccess) {
              app.globalData.userInfo = res.data.Data;
              wx.showModal({
                title: '提示',
                content: "手机号认证成功!",
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    wx.switchTab({ url: '../index/index' });
                  }
                }
              });
            } else {
              wx.showModal({
                title: '提示',
                content: res.data.Message,
                showCancel: false
              });
            }
          }
        });
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})